<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Name_FormItem" prop="Entity.Name">
								<template #label>
									<WtmLabelTooltip label="站点名称" tooltip="网站的名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Name" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_LogoId_FormItem" prop="Entity.LogoId">
								<template #label>
									<WtmLabelTooltip label="站点Logo" tooltip="网站的Logo图片" />
								</template>
								<wtm-upload-file v-model="stateData.vmModel.Entity.LogoId" clearable class="width100"></wtm-upload-file>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>编码设置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_CustomGroupCode_FormItem"
								prop="Entity.CustomGroupCode"
								:rules="[{ required: true, message: '是否自定义部门编码为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="是否自定义部门编码" tooltip="控制部门编码是否可自定义" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.CustomGroupCode" clearable class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_CustomJobPositionCode_FormItem"
								prop="Entity.CustomJobPositionCode"
								:rules="[{ required: true, message: '是否自定义岗位编码为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="是否自定义岗位编码" tooltip="如果开启，则岗位编码可以自定义，默认为自动生成的数字编码" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.CustomJobPositionCode" clearable class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>登录设置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_SmsLogin_FormItem"
								prop="Entity.SmsLogin"
								:rules="[{ required: true, message: '短信登录为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="短信登录" tooltip="是否启用短信登录功能" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.SmsLogin" clearable class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_WechatLogin_FormItem"
								prop="Entity.WechatLogin"
								:rules="[{ required: true, message: '微信登录为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="微信登录" tooltip="是否启用微信登录功能" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.WechatLogin" clearable class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>微信配置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatMpAppId_FormItem" prop="Entity.WechatMpAppId">
								<template #label>
									<WtmLabelTooltip label="微信小程序AppID" tooltip="微信小程序的应用ID" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatMpAppId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatMpAppSecret_FormItem" prop="Entity.WechatMpAppSecret">
								<template #label>
									<WtmLabelTooltip label="微信小程序AppSecret" tooltip="微信小程序的应用密钥" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatMpAppSecret" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatWebAppId_FormItem" prop="Entity.WechatWebAppId">
								<template #label>
									<WtmLabelTooltip label="微信网站AppID" tooltip="微信网站应用的ID" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatWebAppId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatWebSecret_FormItem" prop="Entity.WechatWebSecret">
								<template #label>
									<WtmLabelTooltip label="微信网站AppSecret" tooltip="微信网站应用的密钥" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatWebSecret" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatWebRedirectUri_FormItem" prop="Entity.WechatWebRedirectUri">
								<template #label>
									<WtmLabelTooltip label="微信网站回调地址" tooltip="微信登录的回调地址" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatWebRedirectUri" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatAppAppId_FormItem" prop="Entity.WechatAppAppId">
								<template #label>
									<WtmLabelTooltip label="微信APP应用AppID" tooltip="微信APP应用的应用ID" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatAppAppId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatAppSecret_FormItem" prop="Entity.WechatAppSecret">
								<template #label>
									<WtmLabelTooltip label="微信APP应用AppSecret" tooltip="微信APP应用的密钥" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatAppSecret" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_WechatAppRedirectUri_FormItem" prop="Entity.WechatAppRedirectUri">
								<template #label>
									<WtmLabelTooltip label="微信APP回调地址" tooltip="微信APP登录的回调地址" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.WechatAppRedirectUri" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>短信配置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SmsAppId_FormItem" prop="Entity.SmsAppId">
								<template #label>
									<WtmLabelTooltip label="短信应用ID" tooltip="短信服务提供商的应用ID" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SmsAppId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SmsHost_FormItem" prop="Entity.SmsHost">
								<template #label>
									<WtmLabelTooltip label="短信服务地址" tooltip="短信服务的API主机地址" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SmsHost" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SmsSecretName_FormItem" prop="Entity.SmsSecretName">
								<template #label>
									<WtmLabelTooltip label="短信密钥名称" tooltip="短信服务的密钥名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SmsSecretName" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SmsSecretKey_FormItem" prop="Entity.SmsSecretKey">
								<template #label>
									<WtmLabelTooltip label="短信密钥" tooltip="短信服务的密钥值" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SmsSecretKey" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SmsTemplate_FormItem" prop="Entity.SmsTemplate">
								<template #label>
									<WtmLabelTooltip label="短信模板" tooltip="短信发送时使用的模板内容" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SmsTemplate" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>阿里云配置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_AliyunOcrAppKey_FormItem" prop="Entity.AliyunOcrAppKey">
								<template #label>
									<WtmLabelTooltip label="阿里云OCR AppKey" tooltip="阿里云OCR服务的AppKey" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.AliyunOcrAppKey" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_AliyunOcrAppSecret_FormItem" prop="Entity.AliyunOcrAppSecret">
								<template #label>
									<WtmLabelTooltip label="阿里云OCR AppSecret" tooltip="阿里云OCR服务的AppSecret" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.AliyunOcrAppSecret" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_AliyunOcrAppCode_FormItem" prop="Entity.AliyunOcrAppCode">
								<template #label>
									<WtmLabelTooltip label="阿里云OCR AppCode" tooltip="阿里云OCR服务的AppCode" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.AliyunOcrAppCode" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>人事配置</span>
					</div>
					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SubstitutionMonthlyLimit_FormItem" prop="Entity.SubstitutionMonthlyLimit">
								<template #label>
									<WtmLabelTooltip label="人事替换班月度限制次数" tooltip="人事替换班月度限制次数" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.SubstitutionMonthlyLimit" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>系统设置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_TenantCode_FormItem" prop="Entity.TenantCode">
								<template #label>
									<WtmLabelTooltip label="租户代码" tooltip="多租户系统中的租户唯一标识" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.TenantCode" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysSiteConfigApi as api } from '/@/views/_admin/syssiteconfig/api';
import other from '/@/utils/other';
import { ISysSiteConfigVm } from '/@/views/_admin/syssiteconfig/api/interface';
import { IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const hasSavedOnce = ref(false);
const stateData = reactive<IViewModel<ISysSiteConfigVm>>({
	vmModel: {
		Entity: {
			ControlConfig: null,
			Name: null,
			LogoId: null,
			CustomGroupCode: false,
			CustomJobPositionCode: false,
			SmsLogin: false,
			WechatLogin: false,
			WechatMpAppId: null,
			WechatMpAppSecret: null,
			WechatWebAppId: null,
			WechatWebSecret: null,
			WechatWebRedirectUri: null,
			WechatAppAppId: null,
			WechatAppSecret: null,
			WechatAppRedirectUri: null,
			SmsAppId: null,
			SmsHost: null,
			SmsSecretName: null,
			SmsSecretKey: null,
			SmsTemplate: null,
			AliyunOcrAppKey: null,
			AliyunOcrAppSecret: null,
			AliyunOcrAppCode: null,
			TenantCode: null,
			SubstitutionMonthlyLimit: null,
		},
		ActionName: null,
		IsMyApprove: null,
	},
});

const hasEntityId = computed(() => {
	const entity = stateData.vmModel.Entity as any;
	return !!(entity && entity.ID);
});
const handleApiResponse = (response: any) => {
	ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '提交成功');
	emit('refresh');
	loading.value = false;
	closeDialog();
};

const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

const handleApiRequest = (apiCall: () => Promise<any>, beforeSubmit: () => void, isSubmit = false) => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			beforeSubmit();
			apiCall()
				.then((response: any) => handleApiResponse(response))
				.catch(handleError);
		}
	});
};
// 取消
const onClose = () => {
	closeDialog();
};

// 保存
const onSave = () => {
	handleApiRequest(
		() => {
			// 创建一个深拷贝，确保新增时不发送无效的 ID 字段
			const submitData = JSON.parse(JSON.stringify(stateData.vmModel));
			if (!submitData.Entity.ID) {
				// 如果没有有效的 ID，删除该字段
				delete submitData.Entity.ID;
			}
			return api().add(submitData);
		},
		() => {},
		false
	);
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([]);
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
		hasSavedOnce.value = true;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.SUBMIT,
			type: 'primary',
			onClick: onSave,
			visible: true,
		},
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
